package com.xique.park.controller.web;

import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.park.bean.domain.ParkCarMngtype;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.bean.request.web.mngType.*;
import com.xique.park.constant.ParkApiName;
import com.xique.park.service.biz.service.IParkMngTypeBizService;
import com.xique.park.service.command.service.IParkIpcCommandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆管理类型Controller
 *
 * @author xique
 */
@RestController
@RequestMapping("/web/mngType")
public class WebParkCarMngTypeController {

    @Autowired
    private IParkMngTypeBizService parkMngTypeBizService;

    @Autowired
    private IParkIpcCommandService parkIpcCommandService;

    /**
     * 查询车辆管理类型列表
     */
    @PostMapping("/list")
    @PreAuthorize(hasPermi = "park:mngtype:list")
    public AjaxResult parkCarList(@Validated @RequestBody MngTypeListRequest request) {
        return parkMngTypeBizService.getParkCarMngTypeList(request);
    }

    /**
     * 添加车辆管理类型
     */
    @PreAuthorize(hasPermi = "park:mngtype:add")
    @Log(title = "车辆管理类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addParkCarMngType(@Validated @RequestBody MngTypeAddRequest request) {
        return parkMngTypeBizService.addParkCarMngType(request);
    }

    /**
     * 编辑车辆管理类型信息回显
     */
    @GetMapping("/info")
    @PreAuthorize(hasPermi = "park:mngtype:query")
    public AjaxResult getParkCarMngTypeInfo(Long id) {
        return parkMngTypeBizService.getParkCarMngTypeInfo(id);
    }

    /**
     * 编辑车辆管理类型
     */
    @PreAuthorize(hasPermi = "park:mngtype:edit")
    @Log(title = "车辆管理类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editParkCar(@Validated @RequestBody MngTypeEditRequest request) {
        return parkMngTypeBizService.editParkCarMngType(request);
    }

    /**
     * 删除车辆管理类型
     */
    @PreAuthorize(hasPermi = "park:mngtype:remove")
    @Log(title = "车辆管理类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@Validated @RequestBody MngTypeDeleteRequest request) {
        return parkMngTypeBizService.delParkCarMngType(request);
    }

    /**
     * 批量下发管理类型至设备（下发部分和全部通用）
     */
    @PreAuthorize(hasPermi = "parking:all:online")
    @PostMapping("/downMngType")
    @Log(title = "车辆管理类型", businessType = BusinessType.COMMAND)
    public AjaxResult downMngType(@Validated @RequestBody MngTypeDownRequest request) {
        return parkMngTypeBizService.downMngType(request);
    }

    /**
     * 车场临停类车牌颜色获取无效颜色
     */
    @GetMapping("/getTempInvalidColors")
    @PreAuthorize(hasPermi = "park:mngtype:query")
    public AjaxResult getTempInvalidColors() {
        return parkMngTypeBizService.getTempInvalidColors();
    }

    /**
     * 车辆管理类型状态修改
     */
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPermi = "park:mngtype:edit")
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ParkCarMngtype parkCarMngtype) {
        int count = parkMngTypeBizService.changeStatus(parkCarMngtype);
        if (count > 0) {
            List<ParkCarMngtype> mngtypeList = new ArrayList<>(1);
            parkCarMngtype = parkMngTypeBizService.selectParkCarMngtypeById(parkCarMngtype.getId());
            mngtypeList.add(parkCarMngtype);
            // 下发车辆管理类型信息至设备
            List<ParkDeviceNew> parkDeviceNewList = parkIpcCommandService.getMainDevicesByParkId(parkCarMngtype.getParkId());
            return parkIpcCommandService.downMngType(parkCarMngtype.getParkId(), parkDeviceNewList, mngtypeList, ParkApiName.VER_2_0);
        } else {
            return AjaxResult.error("保存失败");
        }
    }

}
